package com.njwb.day07.user.view;


import com.njwb.day07.user.entity.User;
import com.njwb.day07.user.service.UserService;
import com.njwb.day07.user.service.impl.UserServiceImpl;

import java.util.Scanner;

//视图、与用户交互
public class TestUserView {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("用户注册登录演示");
        while(true){
            System.out.println("1.用户注册");
            System.out.println("2.用户登录");
            System.out.println("3.根据用户名查询");
            System.out.println("4.退出系统");
            System.out.println("请选择：");
            int input=sc.nextInt();
            switch (input){
                case 1:regist();break;
                case 2:login();break;
                case 3:findUserByUsername();break;
                case 4:System.exit(0);break;
            }
        }
    }
    //根据用户名查询用户信息
    public static void findUserByUsername(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.next();
        //调用用户业务接口实例
        UserService userService=new UserServiceImpl();
        User user=userService.findUserByUsername(username);
        if(user!=null){
            System.out.println("用户名\t密码\t电话\t地址");
            System.out.println(user.getUsername()+"\t"+user.getPassword()+"\t"+user.getPhone()+"\t"+user.getAddress());
        }else {
            System.out.println("该用户不存在");
        }
    }

    //注册
    public static void regist(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.next();
        System.out.println("请输入密码：");
        String password=sc.next();
        System.out.println("请输入电话：");
        String phone=sc.next();
        System.out.println("请输入地址：");
        String address=sc.next();
        //将数据封装到用户对象中
        User user=new User(username,password,phone,address);
        //调用用户业务接口实例
        UserService userService=new UserServiceImpl();
        String message=userService.regist(user);
        System.out.println(message);
    }
    //登录
    public static void login(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.next();
        System.out.println("请输入密码：");
        String password=sc.next();
        //调用用户业务接口实例
        UserService userService=new UserServiceImpl();
        String message=userService.login(username,password);
        System.out.println(message);
    }
}
